//
//  MainSearchController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/14.
//

import UIKit

class MainSearchController: CommonViewController {
    private let searchBar = UISearchBar()
    private var historyView:SearchKeyHistoryView!
    private var topKeyView:SearchKeyView!
    private var recommendKeyView : UIView!
    private var selectTip = UIView()
    private var selectHotKeyClosure:((UIButton)->Void)?
    private let line = UIView.SeparatorLine()
    private lazy var wuhenView : NoSearchRecordView={
        let v = NoSearchRecordView()
        view.addSubview(v )
        v.snp.makeConstraints({
            $0.left.top.right.equalTo(0)
            $0.bottom.equalToSuperview().inset(41)
        })
        return v
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = BACKGROUND_COLOR
        self.navigationController?.navigationBar.shadowImage = UIImage()
        let item = UIBarButtonItem.init(title: "搜索", style: .plain, target: self, action: #selector(searchTap))
        item.setTitleTextAttributes([NSAttributedString.Key.font : Font_Size_15], for: .normal)
        item.setTitleTextAttributes([NSAttributedString.Key.font : Font_Size_15], for: .highlighted)
        item.tintColor = B000_COLOR
        self.navigationItem.rightBarButtonItem = item
        searchBar.searchBarStyle = .minimal
        searchBar.placeholder = self.title
        let h = searchBar.heightAnchor.constraint(equalToConstant: navigationBarHeight-UIApplication.shared.statusBarFrame.height)
        h.isActive = true
        h.priority = .defaultHigh
        let w = searchBar.widthAnchor.constraint(equalToConstant: ScreenWidth*0.9)
        w.priority = .defaultHigh
        w.isActive = true
        self.navigationItem.titleView = searchBar
        
        let bottomView = UIView()
        bottomView.backgroundColor = .white
        view.addSubview(bottomView)
        bottomView.snp.makeConstraints({
            $0.height.equalTo(40)
            $0.left.right.bottom.equalTo(0)
        })
        let searchMode = UILabel()
        searchMode.textColor = B999_TITLE_COLOR
        searchMode.text = "无痕搜索模式"
        searchMode.font = Font_Size_13
        bottomView.addSubview(searchMode)
        searchMode.snp.makeConstraints({
            $0.left.equalTo(15)
            $0.centerY.equalToSuperview()
        })
        let modeSwitch = UISwitch()
        modeSwitch.isOn = false
        modeSwitch.addTarget(self, action: #selector(searchModeChange(sw:)), for: .valueChanged)
        bottomView.addSubview(modeSwitch)
        modeSwitch.snp.makeConstraints({
            $0.right.equalToSuperview().inset(15)
            $0.centerY.equalToSuperview()
        })
        let scroll = UIScrollView()
        scroll.backgroundColor = .white
        view.addSubview(scroll)
        scroll.snp.makeConstraints({
            $0.left.top.right.equalTo(0)
            $0.width.equalTo(ScreenWidth)
            $0.bottom.equalTo(bottomView.snp.top).inset(-1)
        })
        
        topKeyView = SearchKeyView(items: [NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false)])
        scroll.addSubview(topKeyView)
        topKeyView.snp.makeConstraints({
            $0.top.equalTo(15)
            $0.left.equalTo(0)
            $0.right.width.equalToSuperview()
        })
        
        scroll.addSubview(line)
        line.snp.makeConstraints({
            $0.top.equalTo(topKeyView.snp.bottom).offset(15)
            $0.left.equalTo(15)
            $0.height.equalTo(1)
            $0.right.equalToSuperview().inset(15)
        })
        
        historyView = SearchKeyHistoryView(items: [NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: true),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: true)])
        scroll.addSubview(historyView)
      
        historyView.snp.makeConstraints({
            $0.top.equalTo(line.snp.bottom)
            $0.left.equalTo(0)
            $0.right.width.equalToSuperview()
        })
        let line2 = UIView.SeparatorLine()
        historyView.addSubview(line2)
        line2.snp.makeConstraints({
            $0.bottom.equalToSuperview()
            $0.left.equalTo(15)
            $0.height.equalTo(1)
            $0.right.equalToSuperview().inset(15)
        })
     
        
        recommendKeyView = UIView()
        selectTip.backgroundColor = B000_COLOR
        recommendKeyView.addSubview(selectTip)
        
        let btnTitles = ["猜你想搜","头条热榜"]
        var temp:UIButton?
        var btns:Array<UIButton> = []
        for i in 0..<btnTitles.count {
            let btn = UIButton(type: .custom)
            btns.append(btn)
            btn.setTitleColor(B000_COLOR, for: .selected)
            btn.setTitleColor(B999_TITLE_COLOR, for: .normal)
            recommendKeyView.addSubview(btn)
            btn.setTitle(btnTitles[i], for: .normal)
            btn.addTarget(self, action: #selector(selectHotKey(btn:)), for: .touchUpInside)
            btn.snp.makeConstraints({
                if temp == nil{
                    $0.left.equalTo(15)
                    selectTip.snp.makeConstraints({
                        $0.centerX.equalTo(btn)
                        $0.width.equalTo(btn).dividedBy(2)
                        $0.height.equalTo(1)
                        $0.top.equalTo(btn.snp.bottom)
                    })
                }else{
                    $0.left.equalTo((temp?.snp.right)!).offset(15)
                }
                $0.top.equalTo(10)
            })
            temp = btn
        }
        
        var tabs:Array<UIView> = []
        let tab1 = SearchKeyView(items: [NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: true),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: true)])
        recommendKeyView.addSubview(tab1)
        tabs.append(tab1)
        tab1.snp.makeConstraints({
            $0.top.equalTo(btns.first!.snp.bottom).offset(15)
            $0.left.right.bottom.equalTo(0)
        })
        let tab2 = SearchHotKeyView(items: [NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: true),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: false),NewsSearchTextModel.init(key: self.title!, isHot: true)])
        recommendKeyView.addSubview(tab2)
        tab2.isHidden = true
        tabs.append(tab2)
        tab2.snp.makeConstraints({
            $0.top.equalTo(btns.first!.snp.bottom).offset(15)
            $0.left.right.equalTo(0)
        })
        
        self.selectHotKeyClosure = { btn in
            for btn in btns {
                btn.isSelected = false
            }
            for v in tabs {
                v.isHidden = true
                v.snp.removeConstraints()
            }
            let index = btns.firstIndex(of: btn)
            tabs[index!].isHidden = false
            tabs[index!].snp.makeConstraints({
                $0.top.equalTo(btns.first!.snp.bottom).offset(15)
                $0.left.right.bottom.equalTo(0)
            })
        }
        btns.first?.isSelected = true
        scroll.addSubview(recommendKeyView)
        recommendKeyView.snp.makeConstraints({
            $0.left.right.equalTo(0)
            $0.bottom.width.equalToSuperview()
            $0.top.equalTo(historyView.snp.bottom)
        })
        
        historyView.allClearBlock = { [weak self] in
            self?.historyView.isHidden = true
            self?.historyView.snp.removeConstraints()
            self?.recommendKeyView.snp.remakeConstraints({
                $0.left.right.equalTo(0)
                $0.bottom.width.equalToSuperview()
                $0.top.equalTo((self?.line.snp.bottom)!)
            })
        }

        let closebtn = UIButton(type: .custom)
        closebtn.setTitle("", for: .normal)
        closebtn.tintColor = B999_TITLE_COLOR
        closebtn.setImage(UIImage(named: "yincang")?.withRenderingMode(.alwaysTemplate), for: .normal)
        closebtn.setTitleColor(B999_TITLE_COLOR, for: .selected)
        closebtn.setTitle("查看全部推荐词", for: .selected)
        closebtn.font = Font_Size_13
        closebtn.setImage(UIImage(named: "xianshi")?.withRenderingMode(.alwaysTemplate), for: .selected)
        closebtn.addTarget(self, action: #selector(hiddenHotKey(btn:)), for: .touchUpInside)
        closebtn.contentEdgeInsets = UIEdgeInsets.init(top: 15, left: 15, bottom: 15, right: 15)
        scroll.addSubview(closebtn)
        closebtn.snp.makeConstraints({
            $0.right.equalTo(0)
            $0.top.equalTo(recommendKeyView)
        })
        
    }
    @objc func selectHotKey(btn:UIButton){
        self.selectHotKeyClosure?(btn)
        btn.isSelected = !btn.isSelected
        selectTip.snp.remakeConstraints({
            $0.centerX.equalTo(btn)
            $0.width.equalTo(btn).dividedBy(2)
            $0.height.equalTo(1)
            $0.top.equalTo(btn.snp.bottom)
        })
    }
    @objc func hiddenHotKey(btn:UIButton){
        btn.isSelected = !btn.isSelected
        recommendKeyView.isHidden = btn.isSelected
        if btn.isSelected {
            btn.snp.remakeConstraints({
                $0.left.right.equalTo(0)
                $0.top.equalTo(historyView.snp.bottom)
            })
        }else{
            btn.snp.remakeConstraints({
                $0.right.equalTo(0)
                $0.top.equalTo(historyView.snp.bottom)
                $0.width.equalTo(50)
            })
        }
    }
    @objc func searchModeChange(sw:UISwitch){
        searchBar.setImage(UIImage(named: sw.isOn ? "wuhen":""), for: .search, state: .normal)
        wuhenView.isHidden = !sw.isOn
        searchBar.placeholder = sw.isOn ? "搜你想看的":self.title!
    }
    override func backBtnClick() {
        self.navigationController?.popViewController(animated: false)
    }
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .default
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        searchBar.becomeFirstResponder()
    }
    
    @objc func searchTap(){
        historyView.setItems(items: [NewsSearchTextModel.init(key: "新的历史", isHot: false)])
        historyView.isHidden = false
        historyView.snp.remakeConstraints({
            $0.top.equalTo(line.snp.bottom)
            $0.left.equalTo(0)
            $0.right.width.equalToSuperview()
        })
        recommendKeyView.snp.remakeConstraints({
            $0.left.right.equalTo(0)
            $0.bottom.width.equalToSuperview()
            $0.top.equalTo(historyView.snp.bottom)
        })
    }
}
